#include "InPerSteeringEngine.h"

#define DEF_TAG         "SteerEngine"

Com_s64 g_InPer_SteerEngine_NS = DEF_PER_SE_DEFAULT_NS;
Com_s64 g_InPer_SteerEngine_Speed = DEF_PER_SE_SPEED_VAL;

void Ext_InPer_SteerEngine_TurnLeft(void)
{
    if(g_InPer_SteerEngine_NS > DEF_PER_SE_MIN_NS)
    {
        g_InPer_SteerEngine_NS -= g_InPer_SteerEngine_Speed;
        Ext_SdkPwm_100Hz_Move(E_CHIP_SDK_PWM2,g_InPer_SteerEngine_NS);
    }
}

void Ext_InPer_SteerEngine_TurnRight(void)
{
    if(g_InPer_SteerEngine_NS < DEF_PER_SE_MAX_NS)
    {
        g_InPer_SteerEngine_NS += g_InPer_SteerEngine_Speed;
        Ext_SdkPwm_100Hz_Move(E_CHIP_SDK_PWM2,g_InPer_SteerEngine_NS);
    }
}

void Ext_InPer_SteerEngine_Turn_Left_Right_Vaule(Com_s64 val)
{
    Ext_SdkPwm_100Hz_Move(E_CHIP_SDK_PWM2,g_InPer_SteerEngine_NS);
}

void InPer_SteerEngine_Init_Center(void)
{
    PRT_INFO(DEF_TAG,"init steer engine\n");
    Ext_SdkPwm_100Hz_Move(E_CHIP_SDK_PWM2,g_InPer_SteerEngine_NS);
}

Com_pvoid InPer_SteerEngine_Thread(Com_pvoid arg)
{
    Ext_SdkPwm_Init(E_CHIP_SDK_PWM2);
    Ext_SdkGpio_Write_SteepEngine(DEF_COM_LEVEL_HIGHT);
    usleep(100 * 1000);

    InPer_SteerEngine_Init_Center();
    usleep(100 * 1000);
    Ext_SdkGpio_Write_SteepEngine(DEF_COM_LEVEL_LOW);

    return NULL;
}

void Ext_InPer_SteerEngine_Init(void)
{
    pthread_t id;

    pthread_create(&id,NULL,InPer_SteerEngine_Thread,NULL);
    pthread_detach(id);
}

void Ext_InPer_SteerEngine_Destroy(void)
{
    Ext_SdkPwm_Destroy(E_CHIP_SDK_PWM2);
}
